package net.shambolica.jport.runner;

import net.erl.objects.ErlTerm;

import java.io.IOException;

/**
 * I am a connection - I pass Erlang Terms to and fro.
 */
public interface DataConnection {

    /**
     * Close the connection.
     * If already closed,
     * @throws java.io.IOException if an IO exception occurs.
     */
    public void close() throws IOException;

    /**
     * Is this connection stopped/closed?
     * @return True if state is STOPPED, false otherwise.
     */
    public boolean isClosed();

    /**
     * Read a term, blocking until done, unless in STOPPED state.
     * If in STOPPED state, null will be returned immediately.
     * @return A received term, or null if in STOPPED state.
     * @throws InterruptedException if operation interrupted. The connection
     * is expected to go into STOPPED state immediately.
     * @throws IOException if any input error occurs. The connection is expected to go
     * into STOPPED state immediately.
     */
    public ErlTerm readTerm() throws InterruptedException, IOException;

    /**
     * Send a term, blocking until done, unless in STOPPED state.
     * If in STOPPED state, will simply return immediately.
     * @param term the term to send.
     * @throws InterruptedException if interrupted. The connection is expected to go
     * into STOPPED state immediately.
     * @throws IOException if any output error occurs. The connection is expected to go
     * into STOPPED state immediately.
     */
    public void writeTerm(ErlTerm  term) throws InterruptedException, IOException;

}
